package cn.stylefeng.guns.modular.emergency.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.roses.kernel.rule.exception.base.ServiceException;
import cn.stylefeng.guns.modular.emergency.entity.EmergencyType;
import cn.stylefeng.guns.modular.emergency.enums.EmergencyTypeExceptionEnum;
import cn.stylefeng.guns.modular.emergency.mapper.EmergencyTypeMapper;
import cn.stylefeng.guns.modular.emergency.pojo.request.EmergencyTypeRequest;
import cn.stylefeng.guns.modular.emergency.service.EmergencyTypeService;
import cn.stylefeng.roses.kernel.db.api.factory.PageFactory;
import cn.stylefeng.roses.kernel.db.api.factory.PageResultFactory;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Date;

/**
 * 应急预案分类表业务实现层
 *
 * @author xbz
 * @date 2023/04/24 14:58
 */
@Service
public class EmergencyTypeServiceImpl extends ServiceImpl<EmergencyTypeMapper, EmergencyType> implements EmergencyTypeService {

	@Override
    public void add(EmergencyTypeRequest emergencyTypeRequest) {
        EmergencyType emergencyType = new EmergencyType();
        BeanUtil.copyProperties(emergencyTypeRequest, emergencyType);
        this.save(emergencyType);
    }

    @Override
    public void del(EmergencyTypeRequest emergencyTypeRequest) {
        EmergencyType emergencyType = this.queryEmergencyType(emergencyTypeRequest);
        this.removeById(emergencyType.getId());
    }

    @Override
    public void edit(EmergencyTypeRequest emergencyTypeRequest) {
        EmergencyType emergencyType = this.queryEmergencyType(emergencyTypeRequest);
        BeanUtil.copyProperties(emergencyTypeRequest, emergencyType);
        this.updateById(emergencyType);
    }

    @Override
    public EmergencyType detail(EmergencyTypeRequest emergencyTypeRequest) {
        return this.queryEmergencyType(emergencyTypeRequest);
    }

    @Override
    public PageResult<EmergencyType> findPage(EmergencyTypeRequest emergencyTypeRequest) {
        LambdaQueryWrapper<EmergencyType> wrapper = createWrapper(emergencyTypeRequest);
        Page<EmergencyType> sysRolePage = this.page(PageFactory.defaultPage(), wrapper);
        return PageResultFactory.createPageResult(sysRolePage);
    }

    @Override
    public List<EmergencyType> findList(EmergencyTypeRequest emergencyTypeRequest) {
        LambdaQueryWrapper<EmergencyType> wrapper = this.createWrapper(emergencyTypeRequest);
        return this.list(wrapper);
    }

    /**
     * 获取信息
     *
     * @author xbz
     * @date 2023/04/24 14:58
     */
    private EmergencyType queryEmergencyType(EmergencyTypeRequest emergencyTypeRequest) {
        EmergencyType emergencyType = this.getById(emergencyTypeRequest.getId());
        if (ObjectUtil.isEmpty(emergencyType)) {
            throw new ServiceException(EmergencyTypeExceptionEnum.EMERGENCY_TYPE_NOT_EXISTED);
        }
        return emergencyType;
    }

    /**
     * 创建查询wrapper
     *
     * @author xbz
     * @date 2023/04/24 14:58
     */
    private LambdaQueryWrapper<EmergencyType> createWrapper(EmergencyTypeRequest emergencyTypeRequest) {
        LambdaQueryWrapper<EmergencyType> queryWrapper = new LambdaQueryWrapper<>();

        Long id = emergencyTypeRequest.getId();
        String categoryName = emergencyTypeRequest.getCategoryName();

        queryWrapper.eq(ObjectUtil.isNotNull(id), EmergencyType::getId, id);
        queryWrapper.like(ObjectUtil.isNotEmpty(categoryName), EmergencyType::getCategoryName, categoryName);

        return queryWrapper;
    }

}